package core.validation;

import core.exceptions.ErrorHandler;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import core.main.WebKeys;

public class Validation implements Filter {

    private FilterConfig filterConfig = null;
    private List<ErrorHandler> le = new ArrayList<ErrorHandler>();
    private ServletContext servletContext;

    public Validation() {
    }

    private void doBeforeProcessing(ServletRequest request, ServletResponse response) throws IOException, ServletException {
    }

    private void doAfterProcessing(ServletRequest request, ServletResponse response) throws IOException, ServletException {
    }

    /**
     *
     * @param request The servlet request we are processing
     * @param response The servlet response we are creating
     * @param chain The filter chain we are processing
     *
     * @exception IOException if an input/output error occurs
     * @exception ServletException if a servlet error occurs
     */
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        doBeforeProcessing(request, response);
        String s = request.getParameter("nume");
        if (s != null && s.equals("d")) {
            le.add(new ErrorHandler("nume", "Mesaj de erroare"));

            remapParameters(request);
            servletContext.setAttribute(WebKeys.ERRORS, le);

            request.getRequestDispatcher("").forward(request, response);
        } else {
            chain.doFilter(request, response);
        }
        doAfterProcessing(request, response);
    }

    private void remapParameters(ServletRequest request) {
        Map<String, String[]> parameters = request.getParameterMap();
        Iterator<String> attributes = parameters.keySet().iterator();
        while (attributes.hasNext()) {
            String attribute = attributes.next();
            Object value = parameters.get(attribute);
            servletContext.setAttribute(attribute, value);
        }
    }

    /**
     * Return the filter configuration object for this filter.
     */
    public FilterConfig getFilterConfig() {
        return (this.filterConfig);
    }

    /**
     * Set the filter configuration object for this filter.
     *
     * @param filterConfig The filter configuration object
     */
    public void setFilterConfig(FilterConfig filterConfig) {
        this.filterConfig = filterConfig;
    }

    /**
     * Destroy method for this filter
     */
    @Override
    public void destroy() {
    }

    /**
     * Init method for this filter
     */
    @Override
    public void init(FilterConfig filterConfig) {
        this.filterConfig = filterConfig;
        servletContext = filterConfig.getServletContext();
    }

    private void sendProcessingError(Throwable t, ServletResponse response) {
        try {
            response.setContentType("text/html");
            PrintStream ps = new PrintStream(response.getOutputStream());
            PrintWriter pw = new PrintWriter(ps);
            pw.print("<html>\n<head>\n<title>Error</title>\n</head>\n<body>\n"); //NOI18N

            // PENDING! Localize this for next official release
            pw.print("<h1>The resource did not process correctly</h1>\n<pre>\n");
            pw.print("</pre></body>\n</html>"); //NOI18N
            pw.close();
            ps.close();
            response.getOutputStream().close();
        } catch (Exception ex) {
        }
    }
}